﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Web.Services.Protocols;
using System.Xml.Serialization;

namespace Wicresoft.EKA.Atrium.WebService
{
    //[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.33440")]
    //[System.SerializableAttribute()]
    //[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
    [XmlRoot("Security", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
    public class Security : SoapHeader//: System.ComponentModel.INotifyPropertyChanged
    {

        public static Security GetSecuritySoapHeader(string username, string password)
        {
            var created = DateTime.Now.ToUniversalTime().ToString("yyyy-mm-ddTHH:mm:ssZ");

            byte[] _nonce = new byte[16];
            RandomNumberGenerator rndGenerator = new RNGCryptoServiceProvider();
            rndGenerator.GetBytes(_nonce);
            return new Security()
            {
                 usernameToken = new UsernameToken()
                   {
                       username = username,
                       password = new PasswordObject()
                       {
                           password = password
                       },
                       nonce = Convert.ToBase64String(_nonce),
                       created = created

                   }
             };
        }


        private XmlSerializerNamespaces xmlns;

        [XmlNamespaceDeclarations]
        public XmlSerializerNamespaces Xmlns
        {
            get
            {
                if (xmlns == null)
                {
                    xmlns = new XmlSerializerNamespaces();
                    xmlns.Add("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                }
                return xmlns;
            }
            set { xmlns = value; }
        }


        private UsernameToken UsernameToken;

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "UsernameToken", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        public UsernameToken usernameToken
        {

            get
            {
                return UsernameToken;
            }
            set
            {
                UsernameToken = value;
            }
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.33440")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
    //[XmlRoot("wsse", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]

    public class UsernameToken
    {

        private string Username;
        private PasswordObject Password;
        private string Nonce;
        private string Created;

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Username", Order = 0, Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        // [XmlAttribute(AttributeName = "wsse:Username")]
        public string username
        {

            get
            {
                return Username;
            }
            set
            {
                Username = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Password", Order = 1)]
        ////Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-If")]
        //[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Password", Order = 1,
        //Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-If#PasswordText")]
        // [XmlAttribute()]
        public PasswordObject password
        {

            get
            {
                return Password;
            }
            set
            {
                Password = value;
            }
        }

        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Nonce", Order = 2, Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        public string nonce
        {

            get
            {
                //return Nonce;
                byte[] _nonce = new byte[16];
                RandomNumberGenerator rndGenerator = new RNGCryptoServiceProvider();
                rndGenerator.GetBytes(_nonce);
                return Convert.ToBase64String(_nonce);
            }
            set
            {
                Nonce = value;
            }
        }
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Created", Order = 3, Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
        public string created
        {

            get
            {
                //return Created;
                //return DateTime.Now.ToString();
                return DateTime.Now.ToUniversalTime().ToString("yyyy-mm-ddTHH:mm:ssZ");
            }
            set
            {
                Created = value;
            }
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.33440")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
    //[XmlRoot("wsse", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
    public class PasswordObject : Object
    {


        private string Password;


        ////[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Password", Order = 1)]
        //////Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-If")]
        //[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Password", Order = 1,
        //Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-If#PasswordText")]

        [XmlText]
        // [XmlAttribute()]
        public string password
        {

            get
            {
                return Password;
            }
            set
            {
                Password = value;
            }
        }

        private string Type = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-If#PasswordText";


        //[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Password", Order = 1)]
        ////Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-If")]
        //[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, ElementName = "Password", Order = 1,
        //Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-If#PasswordText")]

        [XmlAttribute]
        // [XmlAttribute()]
        public string type
        {

            get
            {
                return Type;
            }
            set
            {
                Type = value;
            }
        }
    }
}
